package com.wl.cloud.security.utils;

import com.wl.cloud.core.dto.RestResultDTO;
import com.wl.cloud.core.utils.JsonMapperUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: wanglin
 * @date: 2023-09-12 周二
 * @Version: 1.0
 * @Description: 权限获取工具类
 */
public class SecurityUtils {

    /**
     * security 统一返回信息处理
     *
     * @param response
     * @param code
     * @param msg
     * @throws IOException
     */
    public static void resultSuccessMsg(HttpServletResponse response, Integer code, Object data, String msg) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(code);
        RestResultDTO result = new RestResultDTO<>(code, msg, data, null);
        response.getWriter().write(JsonMapperUtil.toJson(result));
        response.getWriter().flush();
    }

    /**
     * security 统一返回信息处理
     *
     * @param response
     * @param code
     * @param msg
     * @param exceptionMsg
     * @throws IOException
     */
    public static void resultFailMsg(HttpServletResponse response, Integer code, String msg, String exceptionMsg) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(code);
        RestResultDTO result = new RestResultDTO<>(code, msg, null, exceptionMsg);
        response.getWriter().write(JsonMapperUtil.toJson(result));
        response.getWriter().flush();
    }

    /**
     * security 统一返回信息处理
     *
     * @param response
     * @param code
     * @param msg
     * @throws IOException
     */
    public static void resultFailMsg(HttpServletResponse response, Integer code, String msg) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(code);
        RestResultDTO result = new RestResultDTO<>(code, msg, null, null);
        response.getWriter().write(JsonMapperUtil.toJson(result));
        response.getWriter().flush();
    }

    /**
     * security 统一返回信息处理
     *
     * @param response
     * @param msg
     * @throws IOException
     */
    public static void resultFailMsg(HttpServletResponse response, String msg) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        RestResultDTO result = new RestResultDTO<>(0, msg, null, null);
        response.getWriter().write(JsonMapperUtil.toJson(result));
        response.getWriter().flush();
    }
}
